package acwing._1_1AlgorithmBasic._6Greedy;
/**
 * @Project : AlgorithmLearning
 * @Package : ACWing._1_1AlgorithmBasic._6Greedy
 * @File : Q0148.java
 * @Author : WangRuoyu
 * @Date : 2023/4/7 10:38
 */

import java.io.*;
import java.util.PriorityQueue;

public class Q0148 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] words = br.readLine().split(" ");
        int n = Integer.parseInt(words[0]);
        PriorityQueue<Long> pq = new PriorityQueue<>();
        words = br.readLine().split(" ");
        for (int i = 0; i < n; ++i) {
            pq.add(Long.parseLong(words[i]));
        }
        long res = 0;
        while (pq.size() > 1) {
            long a = pq.poll();
            long b = pq.poll();
            res += a + b;
            pq.add(a + b);
        }
        System.out.println(res);
    }
}

